package service;

import utils.DatabaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class SeckillService {

    // 秒杀商品
    public boolean seckillProduct(Long productId, int quantity) throws Exception {
        Connection conn = DatabaseUtil.getConnection("master");
        String sql = "UPDATE product SET available_stock = available_stock - ? WHERE id = ? AND available_stock >= ?";
        try (PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, quantity);
            stmt.setLong(2, productId);
            stmt.setInt(3, quantity);
            return stmt.executeUpdate() > 0;
        }
    }
}